﻿using System;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Okazuki.MVVM.PrismSupport.Interactivity;
using Okazuki.MVVM.PrismSupport.Utils;
using Okazuki.MVVM.PrismSupport.ViewModels;
using wpf_ImageSearch.Model;
using wpf_ImageSearch.Model.YahooApis;

namespace wpf_ImageSearch.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            main.PropertyChanged +=
                (sender, e) => RaisePropertyChanged(e.PropertyName);
        }
        
        private Main main = new Main();

        public Settings Settings
        {
            get
            {
                return main.Settings;
            }
        }

        private string searchQuery;
        public string SearchQuery
        {
            set
            {
                searchQuery = value;
                RaisePropertyChanged(() => SearchQuery);
            }
            get
            {
                return searchQuery;
            }
        }

        public ObservableCollection<ImageSearchResultItem> Items
        {
            get
            {
                return main.Items;
            }
        }

        public long AvailableCount
        {
            get
            {
                return main.AvailableCount;
            }
        }

        public string LastQuery
        {
            get
            {
                return main.LastQuery;
            }
        }

        private ImageSearchResultItem selectedItem;
        public ImageSearchResultItem SelectedItem
        {
            set
            {
                selectedItem = value;
                RaisePropertyChanged(() => SelectedItem);
            }
            get
            {
                return selectedItem;
            }
        }

        private void ShowSearchError(Exception ex)
        {
            ShowErrorMessageRequest.Raise(new Notification()
            {
                Title = "通信エラー",
                Content = ex.Message
            });
        }

        private DelegateCommand search;
        public DelegateCommand Search
        {
            get
            {
                if (search == null)
                    search = new DelegateCommand(
                        () =>
                        {
                            try
                            {
                                main.Search(SearchQuery);
                            }
                            catch (Exception ex)
                            {
                                ShowSearchError(ex);
                            }
                        },
                        () => !string.IsNullOrWhiteSpace(SearchQuery)
                    );
                return search;
            }
        }

        private DelegateCommand getNext;
        public DelegateCommand GetNext
        {
            get
            {
                if (getNext == null)
                    getNext = new DelegateCommand(
                        () =>
                        {
                            try
                            {
                                main.GetNext();
                            }
                            catch (Exception ex)
                            {
                                ShowSearchError(ex);
                            }
                        },
                        () => !string.IsNullOrEmpty(LastQuery)
                    );
                return getNext;
            }
        }

        [AutoInit]
        public InteractionRequest<Notification> ShowPreviewRequest { private set; get; }

        private DelegateCommand showPreview;
        public DelegateCommand ShowPreview
        {
            get
            {
                if (showPreview == null)
                    showPreview = new DelegateCommand(
                        () => this.ShowPreviewRequest.Raise(
                            new Notification()
                            {
                                Title = SelectedItem.Title,
                                Content = SelectedItem
                            }),
                        () => SelectedItem != null
                    );
                return showPreview;
            }
        }

        [AutoInit]
        public InteractionRequest<Notification> ShowErrorMessageRequest { private set; get; }
    }
}
